<?php
/**
 * author:歪妻
 * Date: 2020/11/6 0006
 */


/**
 * @return int|mixed
 * 接收index.php传过来的id
 */
function postId()
{

    if (isset($_GET['post_id'])){
        $postId = $_GET['post_id'];
    }else{
        //总是定义一个id
        $postId = 0;
    }
    return $postId;

}

function getView(PDO $pdo,$postId)
{
    $stmt = $pdo->prepare(
        'SELECT
        title, created_at, body,
        (SELECT COUNT(*) FROM comment WHERE comment.post_id = post.id) comment_count
    FROM
        post
    WHERE
        id = :id'
    );
    if ($stmt === false)
    {
        throw new Exception('准备此查询时出现问题');
    }
    $result = $stmt->execute(
        array( 'id'=> $postId,)
    );
    if ($result === false)
    {
        throw new Exception('运行此查询时出现问题');
    }
    return $stmt->fetch(PDO::FETCH_ASSOC);
}

/**
 * 对特定帖子发表评论
 *
 * @param PDO $pdo
 * @param integer $postId
 * @param array $commentData
 * @return array
 */
function addCommentToPost(PDO $pdo, $postId, array $commentData)
{
    $errors = array();
    // 做一些验证
    if (empty($commentData['name']))
    {
        $errors['name'] = '名称不能为空';
    }
    if (empty($commentData['text']))
    {
        $errors['text'] = '内容不能为空';
    }
    // 如果我们没有错误，请尝试写评论
    if (!$errors)
    {
        $sql = "
            INSERT INTO
                comment
            (name, website, text, created_at,post_id)
            VALUES(:name, :website, :text,:created_at, :post_id)
        ";
        $stmt = $pdo->prepare($sql);
        if ($stmt === false)
        {
            throw new Exception('sql错误不能插入评论');
        }

        $result = $stmt->execute(
            array_merge($commentData, array('post_id' => $postId,'created_at'=>getSqlDateForNow()))
        );
        if ($result === false)
        {
            //todo 会将数据库级别的消息呈现给用户，请解决此问题
            $errorInfo = $stmt->errorInfo();
            if ($errorInfo)
            {
                $errors[] = $errorInfo[2];
            }
        }
    }
    return $errors;
}
/**
 * 称为处理评论表格，成功重定向
 *
 * @param PDO $pdo
 * @param integer $postId
 * @param array $commentData
 */
function handleAddComment(PDO $pdo, $postId, array $commentData)
{
    $errors = addCommentToPost(
        $pdo,
        $postId,
        $commentData
    );
    // 如果没有错误，请重定向回自身并重新显示
    if (!$errors)
    {
        redirectAndExit('view-post.php?post_id=' . $postId);
    }
    return $errors;
}

/**
 * 被称为处理删除评论表格，之后重定向
 *
 * $deleteResponse 数组形式为:
 *
 *	Array ( [6] => Delete )
 *
 * 直接来自这种形式的输入元素:
 *
 *	name="delete-comment[6]"
 *
 * @param PDO $pdo
 * @param integer $postId
 * @param array $deleteResponse
 */
function handleDeleteComment(PDO $pdo, $postId, array $deleteResponse)
{
    if (isLoggedIn())
    {
        $keys = array_keys($deleteResponse);
        $deleteCommentId = $keys[0];
        if ($deleteCommentId)
        {
            deleteComment($pdo, $postId, $deleteCommentId);
        }
        redirectAndExit('view-post.php?post_id=' . $postId);
    }
}


/**
 * 在指定的帖子上删除指定的评论
 *
 * @param PDO $pdo
 * @param integer $postId
 * @param integer $commentId
 * @return boolean 如果命令执行没有错误，则为真
 * @throws Exception
 */
function deleteComment(PDO $pdo, $postId, $commentId)
{
    // id本身就足够了，但是post_id是一个很好的额外安全检查
    $sql = "
        DELETE FROM
            comment
        WHERE
            post_id = :post_id
            AND id = :comment_id
    ";
    $stmt = $pdo->prepare($sql);
    if ($stmt === false)
    {
        throw new Exception('删除出现问题');
    }
    $result = $stmt->execute(
        array(
            'post_id' => $postId,
            'comment_id' => $commentId,
        )
    );
    return $result !== false;
}